@<template>
  <div>
    <!-- 面包屑导航 -->
    <breadcrumb :breadInfo="breadInfo" />

    <!-- 卡片试图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              @click="getGoodsList"
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="toAddPage" type="primary">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品信息展示区 -->
      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name"  label="商品名称"> </el-table-column>
        <el-table-column width="110px" prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column
          width="100px"
          class="table-col"
          prop="goods_weight"
          label="商品重量"
        >
        </el-table-column>
        <el-table-column
          width="100px"
          class="table-col"
          prop="add_time"
          label="创建时间"
        >
        <template slot-scope="scope">
          {{scope.row.add_time|timeFormate}}
        </template>
        </el-table-column>
        <el-table-column width="150px" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              class="el-icon-edit"
              @click="editGoods(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              class="el-icon-delete"
              @click="deleteGoods(scope.row)"
              
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页区 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[2, 4, 8, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>

    <!-- 编辑商品对话框 -->

  </div>
</template>

<script>
import Breadcrumb from "../../common/breadcrumb/Breadcrumb.vue";

export default {
  components: { Breadcrumb },
  data() {
    return {
      breadInfo: ["商品管理", "商品列表"],
      // 查询对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      goodsList: [],
      total: 0,
      // 商品对象
      goodsObj: { }
    };
  },
  created() {
    this.getGoodsList();
  },
  // 请求商品列表
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200){
        return this.$message.error("获取商品数据失败");

      }
      this.goodsList = res.data.goods;
      this.total = res.data.total
    
    },
    // 页码大小发生变化
    handleSizeChange(pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getGoodsList()
    },
    // 当前页发生变化
    handleCurrentChange(pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getGoodsList()
    },
    editGoods(row) {
      
    },
    // 删除商品
    deleteGoods(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          // 发送删除请求
          const {data: res } = await this.$http.delete(`goods/${row.goods_id}`)
          if (res.meta.status !== 200) return this.$message.error("删除失败")
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          // 更新数据
          this.getGoodsList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    toAddPage() {
      this.$router.push('/goods/add')
    }
  },
  
};
</script>

<style lang="less" scoped>
.el-button {
  margin-left: 10px;
}
</style> 